package routers

import (
	"gin-demo01/controller/admin"
	"gin-demo01/controller/api"
	"github.com/gin-gonic/gin"
	"net/http"
)

type Article struct {
	Title   string
	Content string
}

type UserInfo struct {
	Username string `json:"username" form:"username"`
	Password string `json:"password" form:"password"`
}

func RouterInit(router *gin.Engine) {
	r := router.Group("/")
	{
		r.GET("/", api.ApiController{}.Test)
		r.GET("/session", api.ApiController{}.SetSession)
		r.GET("/getSession", api.ApiController{}.GetSession)
		r.GET("/users", api.ApiController{}.Html)
		r.GET("/getUser", func(c *gin.Context) {
			user := &UserInfo{}
			if err := c.ShouldBind(&user); err == nil {
				c.JSON(http.StatusOK, user)
			} else {
				c.JSON(http.StatusBadRequest, gin.H{
					"err": err.Error(),
				})
			}
		})
		r.GET("/user/:id", func(c *gin.Context) {
			id := c.Param("id")
			c.JSON(http.StatusOK, gin.H{
				"id": id,
			})
		})
		r.POST("/doAddUser", func(c *gin.Context) {
			user := &UserInfo{}
			if err := c.ShouldBind(&user); err == nil {
				c.JSON(http.StatusOK, user)
			} else {
				c.JSON(http.StatusBadRequest, gin.H{
					"err": err.Error(),
				})
			}
		})

		r.GET("/ping", api.ApiController{}.Ping)
		r.GET("/delCookie", api.ApiController{}.DeleteCookie)
		r.GET("/xml", func(c *gin.Context) {
			c.XML(http.StatusOK, gin.H{
				"message": "成功",
				"success": true,
			})
		})
		r.GET("/news", func(c *gin.Context) {
			news := &Article{
				Title:   "微信拒收苹果税",
				Content: "马化腾牛逼",
			}
			c.HTML(http.StatusOK, "default/news.html", gin.H{
				"title": "新闻标题",
				"news":  news,
				"score": 89,
				"date":  1725329576,
			})
		})
		r.GET("/admin/news", func(c *gin.Context) {
			news := &Article{
				Title:   "微信拒收苹果税",
				Content: "马化腾牛逼",
			}
			c.HTML(http.StatusOK, "admin/index.html", gin.H{
				"title": "管理员界面",
				"news":  news,
			})
		})
		r.GET("/goods", func(c *gin.Context) {
			c.HTML(http.StatusOK, "default/goods.html", gin.H{
				"title": "商品标题",
				"hobby": []string{"吃饭", "睡觉", "敲代码"},
			})
		})
	}
	adminRouter := router.Group("/admin")
	{
		adminRouter.GET("/user/add", admin.AdminController{}.Add)
		adminRouter.POST("/user/doUpload", admin.AdminController{}.Upload)
		adminRouter.GET("/user/edit", admin.AdminController{}.Edit)
		adminRouter.POST("/user/doEdit", admin.AdminController{}.DoEdit)
		adminRouter.GET("/user/addAll", admin.AdminController{}.AddAll)
		adminRouter.POST("/user/doUploadAll", admin.AdminController{}.DoUploadAll)

		adminRouter.GET("/list", admin.UserController{}.Index)
		adminRouter.POST("/add", admin.UserController{}.Add)
		adminRouter.PUT("/edit", admin.UserController{}.Edit)
		adminRouter.DELETE("/del", admin.UserController{}.Del)
		adminRouter.GET("/item/:id", admin.UserController{}.GetById)

		adminRouter.GET("/article/list", admin.ArticleController{}.Index)

		adminRouter.GET("/bank", admin.BankController{}.Index)
	}
}
